<?php

/**
 * SMTPForm class.
 * SMTPForm is the data structure for keeping
 * web smtp form data. It is used by the 'smtp' action of 'OptionsController'.
 */
class SMTPForm extends Options
{
    public $smtp_host;
    public $smtp_port = 25;
    public $smtp_username;
    public $smtp_nickname;
    public $smtp_password;
    public $smtp_encryption = 'tls';

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// website_title and website_url are required
			array('smtp_host, smtp_username', 'required'),
			// 验证字符串 string
			//array('website_title, website_email, website_language, website_icp, website_statics_script, website_timezone, website_url, seo_title, seo_keywords, seo_description', 'boolean'),
			array('smtp_port', 'numerical', 'integerOnly'=>true, 'min'=>0, 'max'=>65535),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'smtp_host' => Yii::t('backend', 'SMTP Host'),
            'smtp_port' => Yii::t('backend', 'SMTP Port'),
            'smtp_username' => Yii::t('backend', 'SMTP Username'),
            'smtp_nickname' => Yii::t('backend', 'Sender'),
            'smtp_password' => Yii::t('backend', 'SMTP Password'),
            'smtp_encryption' => Yii::t('backend', 'Encryption'),
		);
	}


    /**
     * 填充smtp配置
     *
     */
    public function getSmtpConfig()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name'=>$name));
            if ($model != null) {
                $this->$name = $model->value;
            } else {
                $this->$name = '';
            }
        }
    }


    /**
     * 更新邮箱配置
     * @return bool
     * @throws CException
     */
    public function setSmtpConfig()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name'=>$name));
            if ($model != null) {
                $model->value = $_POST['SMTPForm'][''.$name.''];
                $result = $model->save(false);
            } else {
                $model = Yii::createComponent( Options::className() );
                $model->name = $name;
                $model->value = $this->$name;
                $result = $model->save(false);
            }
            if ($result == false) {
                return $result;
            }
        }
        return true;
    }

    /**
     * 获取smtp邮箱配置
     * @return array
     * @throws CException
     */
    public static function getComponentConfig()
    {
        $config = Yii::createComponent( self::className() );
        $config->getSmtpConfig();
        return [
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => $config->smtp_host,
                'username' => $config->smtp_username,
                'password' => $config->smtp_password,
                'port' => $config->smtp_port,
                'encryption' => $config->smtp_encryption,
            ],
            'messageConfig' => [
                'charset' => 'UTF-8',
                'from' => [$config->smtp_username => $config->smtp_nickname]
            ],
        ];
    }


}
